class IUP_FRAME
-- Creates a native container, which draws a frame with a title around 
-- its child.

inherit
	IUP_CONTAINER
		redefine
			execute_map,
			execute_unmap,
			execute_destroy,
			execute_focus
		end
	IUP_WIDGET_BGCOLOR
	IUP_WIDGET_FGCOLOR
	IUP_WIDGET_EXPAND
	IUP_WIDGET_TITLE
	IUP_WIDGET_ACTIVE
	IUP_WIDGET_FONT
	IUP_WIDGET_SCREENPOSITION
	IUP_WIDGET_POSITION
	IUP_WIDGET_CLIENTSIZE
	IUP_WIDGET_CLIENTOFFSET
	IUP_WIDGET_MAXMIN_SIZE
	IUP_WIDGET_SIZE
	IUP_WIDGET_RASTERSIZE
	IUP_WIDGET_USERSIZE
	IUP_WIDGET_ZORDER
	IUP_WIDGET_VISIBLE
	IUP_WIDGET_CHILD
	IUP_WIDGET_NAME
	IUP_WIDGET_CUSTOM_ATTRIBUTES

create {ANY}
	frame_empty,
	frame

feature {ANY}

	frame_empty
	   -- Create an empty frame
		local
			p, a_frame: POINTER
		do
			a_frame := int_frame (p)
			set_widget(a_frame)
		end
	
	frame (child: IUP_WIDGET)
	   -- Create a new frame containing the widget.
		local
         a_frame: POINTER
		do
			a_frame := int_frame (child.widget)
			set_widget(a_frame)
		end

	-- Attributes
	
	set_childoffset (horizontal, vertical: INTEGER)
		require
			horizontal >= 0
			vertical >= 0
		local
			offset: STRING
		do
			offset := horizontal.out
			offset.append_string("x")
			offset.append_string(vertical.out)

			iup_open.set_attribute(Current, "CHILDOFFSET", offset)
		end

	get_childoffset: TUPLE[INTEGER, INTEGER]
		-- Return the offset of the child.
		local
			offset: STRING
		do
			offset := iup_open.get_attribute(Current, "CHILDOFFSET")
			Result := components_of_size(offset)
		end

	set_sunken (state: BOOLEAN)
		-- When not using a title, the frame line defines a sunken area (lowered 
		-- area). Default: False.
		do
			iup_open.set_attribute(Current, "SUNKEN", boolean_to_yesno(state))
		end

	-- Callbacks
	set_cb_map (act: detachable FUNCTION[TUPLE[IUP_FRAME], STRING])
		-- Called right after an element is mapped and its attributes updated.
		local
			operation: INTEGER
		do
			cb_map := act
			
			if cb_map /= Void then
				operation := 1
			else
				operation := 0
			end
						
			iup_open.set_callback (Current, "MAP_CB", "NONEEDED", operation)
		end

	set_cb_unmap (act: detachable FUNCTION[TUPLE[IUP_FRAME], STRING])
		-- Called right before an element is unmapped.
		local
			operation: INTEGER
		do
			cb_unmap := act

			if cb_unmap /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "UNMAP_CB", "NONEEDED", operation)
		end

	set_cb_destroy (act: detachable FUNCTION[TUPLE[IUP_FRAME], STRING])
		-- Called right before an element is destroyed.
		local
			operation: INTEGER
		do
			cb_destroy := act

			if cb_destroy /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "DESTROY_CB", "NONEEDED", operation)
		end

	set_cb_focus (act: detachable FUNCTION[TUPLE[IUP_FRAME, INTEGER], STRING])
		-- Called when a child of the container gets or looses the focus. It is 
		-- called only if PROPAGATEFOCUS is defined in the child.
		local
			operation: INTEGER
		do
			cb_focus := act

			if cb_focus /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "FOCUS_CB", "NONEEDED", operation)
		end

feature {ANY}

	execute_map: STRING
		do
			if attached cb_map as int_cb then
				Result := int_cb.item([Current])
			else
				Result := "IUP_DEFAULT"
			end
		end

	execute_unmap: STRING
		do
			if attached cb_unmap as int_cb then
				Result := int_cb.item([Current])
			else
				Result := "IUP_DEFAULT"
			end
		end

	execute_destroy: STRING
		do
			if attached cb_destroy as int_cb then
				Result := int_cb.item([Current])
			else
				Result := "IUP_DEFAULT"
			end
		end

	execute_focus (focus: INTEGER): STRING
		do
			if attached cb_focus as int_cb then
				Result := int_cb.item([Current, focus])
			else
				Result := "IUP_DEFAULT"
			end
		end

feature {NONE}

	-- For callbacks
	cb_map: detachable FUNCTION[TUPLE[IUP_FRAME], STRING]
	cb_unmap: detachable FUNCTION[TUPLE[IUP_FRAME], STRING]
	cb_destroy: detachable FUNCTION[TUPLE[IUP_FRAME], STRING]
	cb_focus: detachable FUNCTION[TUPLE[IUP_FRAME, INTEGER], STRING]

	-- Internals
	
	int_frame (child: POINTER): POINTER
		external
			"C inline use %"eiffel-iup.h%""
      alias
			"return IupFrame ($child);"
      end
	
end

-- The MIT License (MIT)

-- Copyright (c) 2016, 2017, 2019, 2020 by German A. Arias

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in 
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
